#include "CCyberSpaceState.h"
#include "CPauseState.h"
#include "COptionsState.h"
#include "ObjectFactory.h"
#include "ObjectManager.h"
#include "CPrintBitmapToScreen.h"
#include "Game Objects/CPlayer.h"
#include "Game Objects/CPistolBullet.h"
#include "Game Objects/CShotgunBullet.h"
#include "Game Objects/CRocketLauncherBullet.h"
#include "Game Objects/CTurret.h"
#include "Game Objects/CSpider.h"
#include "Game Objects/CDrop.h"
#include "Game Objects/CCamera.h"
#include "Game Objects\CTeleporter.h"
#include "Game Objects\CGrenade.h"
#include "CLooseState.h"
#include "CAnimationManager.h"
#include "CSpaceStationState.h"
#include "CWinState.h"

CCyberSpaceState::CCyberSpaceState(void)
{
	m_fPlayerMaxHealth = 100;
	m_fPlayerHealth = m_fPlayerMaxHealth;

	m_fFlashTimer = 0.0f;
	///////////////
	// Events
	CSGP_EventSystem::GetInstance()->RegisterClient("bullet.fired", this);
	CSGP_EventSystem::GetInstance()->RegisterClient("bullet.destroyed", this);

	m_pTM = NULL;
	m_pD3D = NULL;

	m_nPercentLoaded = 0;
	m_fSpeedUpgrade = 50.0f;
}

CCyberSpaceState::CCyberSpaceState(const CCyberSpaceState&)
{

}

CCyberSpaceState& CCyberSpaceState::operator=(const CCyberSpaceState&)
{
	return *this;
}

CCyberSpaceState::~CCyberSpaceState(void)
{
	
	// Unregister classes from object factory
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CGrenade");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CPistolBullet");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CShotgunBullet");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CRocketLauncherBullet");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CTurret");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CSpider");
	ObjectFactory<string, CBase>::GetInstance()->UnregisterClassType("CDrop");
	CSGP_EventSystem::GetInstance()->UnregisterClient("bullet.destroyed", this);
}

CCyberSpaceState* CCyberSpaceState::GetInstance(void)
{
	static CCyberSpaceState instance;
	return &instance;
}

void CCyberSpaceState::Enter(void)
{
	// The state is loading
	m_bIsLoading = true;

	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();

	// Initialize the message system
	CSGP_MessageSystem::GetInstance()->InitMessageSystem(CCyberSpaceState::MessageProc);
	CSGP_EventSystem::GetInstance()->ClearEvents();
	// Load Textures
	m_nLoadingID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/Art Assets/cyberScape_logo.png");
	m_nPercentLoaded = 12;
	DisplayLoading();
	m_nHUDID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/Art Assets/HUD.png");
	m_nPercentLoaded = 25;
	DisplayLoading();
	m_nHealthID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_HealthBar.png");
	m_nPercentLoaded = 37;
	DisplayLoading();
	m_nBackgroundID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_CSBackground.png");
	m_nPercentLoaded = 50;
	DisplayLoading();

	// Load Sounds
	m_nMusicID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_Course.mp3");
	m_nPercentLoaded = 62;
	DisplayLoading();
	m_nPistolSelectID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_PistolSelect.wav");
	m_nPercentLoaded = 75;
	DisplayLoading();
	m_nShotgunSelectID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_ShotgunSelect.wav");
	m_nPercentLoaded = 87;
	DisplayLoading();
	m_nRocketSelectID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_RocketSelect.wav");
	m_nPercentLoaded = 100;
	DisplayLoading();
	m_nReticleID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Reticle.png");
	
	// Set sound volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nPistolSelectID, COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nShotgunSelectID, COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nRocketSelectID, COptionsState::GetInstance()->GetSFXVolume());
	// Set the volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nMusicID, COptionsState::GetInstance()->GetVolume());

	// Start the music
	CSGD_FModManager::GetInstance()->PlaySoundA(m_nMusicID);

	// Load the appropriate audio log for the level
	if(m_nCurrRoom == OFFICE)
	{

	}
	else if(m_nCurrRoom == SECURITY)
	{

	}
	else if(m_nCurrRoom == SNACK)
	{

	}
	else if(m_nCurrRoom == SEC_CONTROL)
	{

	}
	else if(m_nCurrRoom == MAIN_CONTROL)
	{

	}
	else if(m_nCurrRoom == POD)
	{

	}

	// Set notification flags
	m_bPistolAmmo = false;
	m_bShotAmmo = false;
	m_bRocketAmmo = false;

	// Set notification timers
	m_fPistolTimer = 0.0f;
	m_fShotgunTimer = 0.0f;
	m_fRocketTimer = 0.0f;

	//GrenadeTimer
	m_fGrenadeTimer = 0.0f;

	// Set the number of nodes in the map
	m_nNumNodes = map->getNodes()->size()-1;

	// Set the mini-maps map and ai
	m_cmMap.SetAI(&AI);
	
	m_cmMap.SetMap(map);

	//////////////////////
	// Register Classes
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CPistolBullet>("CPistolBullet");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CShotgunBullet>("CShotgunBullet");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CRocketLauncherBullet>("CRocketLauncherBullet");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CTurret>("CTurret");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CSpider>("CSpider");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CDrop>("CDrop");
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CGrenade>("CGrenade");

	// Create the player object.
	if(!m_pPlayer) // If the player is not already present
	{
		m_pPlayer = CPlayer::GetInstance();
		m_pPlayer->SetPosX(200);
		m_pPlayer->SetPosY(200);
		m_pPlayer->SetVelX(0.0f);
		m_pPlayer->SetVelY(0.0f);
		m_pPlayer->SetType(CBase::OBJ_PLAYER);

		ObjectManager::GetInstance()->AddObject(m_pPlayer);
		CCyberSpaceState::GetInstance()->SetPlayer(m_pPlayer);
	}
	m_fPlayerHealth = 100;
	// Give the player ammo to shoot with
	m_pPlayer->SetPistolAmmo(50);
	m_pPlayer->SetShotgunAmmo(30);
	m_pPlayer->SetRocketAmmo(10);
	// Give the player grenades
	m_pPlayer->SetExGrenades(2);
	m_pPlayer->SetFwGrenades(2);

	// Create the camera object
	if(!m_pCamera)
	{
		m_pCamera = CCamera::GetInstance();
	}

	/////////////////////////////
	// Create turrets for level
	
	

	//load a Effect
	
	effects.LoadEffect(".\\Resource\\XML\\bait.xml");
	effects.Play(0, CPlayer::GetInstance());

	//set player health
	CPlayer::GetInstance()->SetMaxHealth(100);
	CPlayer::GetInstance()->sethealth(CPlayer::GetInstance()->GetMaxHealth());

	// Set the player weapon
	m_nCurrWeapon = PISTOL;
	// Set the player's grenade
	m_nCurrGrenade = EX_GRENADE;


	
	for(unsigned int i = 0; i < map->getNodes()->size(); ++i)
	{
		map->getNodes()->operator[](i)->SetAI(&AI);
	}

	map->getNodes()->operator[](map->getNodes()->size() - 1)->SetOwner(OWNER_AI);
	map->getNodes()->operator[](0)->SetOwner(OWNER_PLAYER);
	AI.SetHome(map->getNodes()->operator[](map->getNodes()->size() - 1));
	AI.SetMap(map);
	AI.SetTartget(map->getNodes()->operator[](0));

	for(unsigned int i = 0; i < map->getNodes()->size(); i++)
	{
		map->getNodes()->operator[](i)->SpawnEnemies();
	}
	map->getNodes()->operator[](0)->EnterNode();

	//m_fPlayerHealth = 100;

	//load animations
	PistolDownAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolDown.xml",D3DCOLOR_XRGB(34,190,34));
	PistolUpAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolUp.xml",D3DCOLOR_XRGB(34,190,34));
	PistolLeftAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolLeft.xml",D3DCOLOR_XRGB(34,190,34));

	ShotgunDownAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunDown.xml",D3DCOLOR_XRGB(34,190,34));
	ShotgunUpAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunUp.xml",D3DCOLOR_XRGB(34,190,34));
	ShotgunLeftAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunLeft.xml",D3DCOLOR_XRGB(34,190,34));

	RocketDownAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketDown.xml",D3DCOLOR_XRGB(34,190,34));
	RocketUpAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketUp.xml",D3DCOLOR_XRGB(34,190,34));
	RocketLeftAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketLeft.xml",D3DCOLOR_XRGB(34,190,34));


	PistolShootDownAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolShootDown.xml",D3DCOLOR_XRGB(34,190,34));
	PistolShootLeftAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolShootLeft.xml",D3DCOLOR_XRGB(34,190,34));
	PistolShootUpAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PistolShootUp.xml",D3DCOLOR_XRGB(34,190,34));

	ShotgunShootDownAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunShootDown.xml",D3DCOLOR_XRGB(34,190,34));
	ShotgunShootLeftAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunShootLeft.xml",D3DCOLOR_XRGB(34,190,34));
	ShotgunShootUpAnimID  = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ShotgunShootUp.xml",D3DCOLOR_XRGB(34,190,34));

	RocketShootDownAnimID	= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketShootDown.xml",D3DCOLOR_XRGB(34,190,34));
	RocketShootLeftAnimID	= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketShootLeft.xml",D3DCOLOR_XRGB(34,190,34));
	RocketShootUpAnimID		= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/RocketShootUp.xml",D3DCOLOR_XRGB(34,190,34));

	ThrowGrenadeUpID	= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ThrowGrenadeUp.xml",D3DCOLOR_XRGB(34,190,34));
	ThrowGrenadeLeftID	= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ThrowGrenadeLeft.xml",D3DCOLOR_XRGB(34,190,34));
	ThrowGrenadeDownID	= CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/ThrowGrenadeDown.xml",D3DCOLOR_XRGB(34,190,34));

	Movingdown = false;
	Movingup = false;
	Movingright = false;
	Movingleft = false;

	
	ShootingUp= false; ShootingDown= false; ShootingLeft= false; ShootingRight= false;
	GrenadeDown = false; GrenadeRight = false; GrenadeUp= false; GrenadeLeft= false;

	m_bIsLoading = false;
}

void CCyberSpaceState::DisplayLoading(void)
{
	m_pD3D->Clear(0, 0, 0);
	m_pD3D->DeviceBegin();
	m_pD3D->SpriteBegin();

	m_pTM->Draw(m_nLoadingID, 0, 0, 0.67f, 0.6f);

	if(m_nPercentLoaded == 12)
		CPrintBitmapFontToScreen::GetInstance()->Print("12%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 25)
		CPrintBitmapFontToScreen::GetInstance()->Print("25%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 37)
		CPrintBitmapFontToScreen::GetInstance()->Print("37%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 50)
		CPrintBitmapFontToScreen::GetInstance()->Print("50%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 62)
		CPrintBitmapFontToScreen::GetInstance()->Print("62%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 75)
		CPrintBitmapFontToScreen::GetInstance()->Print("75%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 87)
		CPrintBitmapFontToScreen::GetInstance()->Print("87%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	else if(m_nPercentLoaded == 100)
		CPrintBitmapFontToScreen::GetInstance()->Print("100%", 200, 300, 2.0f, D3DCOLOR_XRGB(255, 0, 0));


	m_pD3D->SpriteEnd();
	m_pD3D->DeviceEnd();
	m_pD3D->Present();
}

bool CCyberSpaceState::Input(void)
{
	// Movement keys
	{
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_UP))
		{
			Movingup = true;
			float speed = -200.0f;
			speed *= 1 + m_pPlayer->GetSpeedFactor();
			if(CPlayer::GetInstance()->GetSpeedActive())
				m_pPlayer->SetVelY(speed - m_fSpeedUpgrade);
			else
				m_pPlayer->SetVelY(speed);
		}
		else
			Movingup = false;

		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_DOWN))
		{
			Movingdown = true;
			float speed = 200.0f;
			speed *= 1 + m_pPlayer->GetSpeedFactor();
			if(CPlayer::GetInstance()->GetSpeedActive())
				m_pPlayer->SetVelY(speed + m_fSpeedUpgrade);
			else
				m_pPlayer->SetVelY(speed);
		}
		else
			Movingdown = false;

		if(!(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_UP)) && !(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_DOWN)))
			m_pPlayer->SetVelY(0.0f);

		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT))
		{
			Movingleft = true;
			float speed = -200.0f;
			speed *= 1 + m_pPlayer->GetSpeedFactor();
			if(CPlayer::GetInstance()->GetSpeedActive())
				m_pPlayer->SetVelX(speed - m_fSpeedUpgrade);
			else
				m_pPlayer->SetVelX(speed);
		}
		else
			Movingleft = false;
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT))
		{
			Movingright = true;
			float speed = 200.0f;
			speed *= 1 + m_pPlayer->GetSpeedFactor();
			if(CPlayer::GetInstance()->GetSpeedActive())
				m_pPlayer->SetVelX(speed + m_fSpeedUpgrade);
			else
				m_pPlayer->SetVelX(speed);
		}
		else
			Movingright = false;
		if(!(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT)) && !(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT)))
			m_pPlayer->SetVelX(0.0f);

		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_G) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(5))
		{
			if(CSGD_DirectInput::GetInstance()->MouseGetPosY() < CPlayer::GetInstance()->GetPosY() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX()) < 50 && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) < 50 )
			GrenadeUp = true;

			else if(CSGD_DirectInput::GetInstance()->MouseGetPosY() > CPlayer::GetInstance()->GetPosY() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX()) < 50 && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) < 50)
			GrenadeDown = true;
		

			else if(CSGD_DirectInput::GetInstance()->MouseGetPosX() < CPlayer::GetInstance()->GetPosX() && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) > 50)
			GrenadeLeft = true;
		

			else if(CSGD_DirectInput::GetInstance()->MouseGetPosX() > CPlayer::GetInstance()->GetPosX() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX())>50 )
			GrenadeRight = true;
		}
	}
	
	if(CSGD_DirectInput::GetInstance()->MouseButtonDown(0) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(2))
	{
		if(CSGD_DirectInput::GetInstance()->MouseGetPosY() < CPlayer::GetInstance()->GetPosY() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX()) < 50 && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) < 50 )
			ShootingUp = true;

		else if(CSGD_DirectInput::GetInstance()->MouseGetPosY() > CPlayer::GetInstance()->GetPosY() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX()) < 50 && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) < 50)
			ShootingDown = true;
		

		else if(CSGD_DirectInput::GetInstance()->MouseGetPosX() < CPlayer::GetInstance()->GetPosX() && (CPlayer::GetInstance()->GetPosX()-CSGD_DirectInput::GetInstance()->MouseGetPosX()) > 50)
			ShootingLeft = true;
		

		else if(CSGD_DirectInput::GetInstance()->MouseGetPosX() > CPlayer::GetInstance()->GetPosX() && (CSGD_DirectInput::GetInstance()->MouseGetPosX()-CPlayer::GetInstance()->GetPosX())>50 )
			ShootingRight = true;
	}
	if(CSGD_DirectInput::GetInstance()->MouseButtonReleased(0) || CSGD_DirectInput::GetInstance()->JoystickButtonReleased(2))
	{
		ShootingUp = false;
		ShootingDown = false;
		ShootingLeft = false;
		ShootingRight = false;
	}


	// Weapon switching keys
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(4))
	{
		if(m_nCurrWeapon > 0)
			m_nCurrWeapon--;
		else
			m_nCurrWeapon = PISTOL;

		if(m_nCurrWeapon == PISTOL)
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nPistolSelectID);
		else if(m_nCurrWeapon == SHOTGUN)
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nShotgunSelectID);
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1))
	{
		if(m_nCurrWeapon < ROCKETS)
			m_nCurrWeapon++;
		else
			m_nCurrWeapon = ROCKETS;

		if(m_nCurrWeapon == SHOTGUN)
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nShotgunSelectID);
		else if(m_nCurrWeapon == ROCKETS)
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nRocketSelectID);
	}

	// Switch grenades
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_TAB)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(3))
	{
		if(m_nCurrGrenade < FW_GRENADE)
			m_nCurrGrenade++;
		else 
			m_nCurrGrenade = EX_GRENADE;
	}

	// Health bar testing
	/*if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_H))
	{
		if(m_fPlayerHealth >= 0)
			m_fPlayerHealth -= 25.6f;
		CPlayer::GetInstance()->sethealth(CPlayer::GetInstance()->gethealth() - 10);
	}*/

	// Go to the pause menu
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE) || CSGD_DirectInput::GetInstance()->KeyPressed(DIK_P)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
	}
	if((CSGD_DirectInput::GetInstance()->KeyDown(DIK_RMENU)&&CSGD_DirectInput::GetInstance()->KeyPressed(DIK_TAB ))
		||((CSGD_DirectInput::GetInstance()->KeyDown(DIK_LMENU)&&CSGD_DirectInput::GetInstance()->KeyPressed(DIK_TAB ))))
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
	}

	return true;
}

void CCyberSpaceState::Update(float fElapsedTime)
{
	// Update objects in object manager
 	ObjectManager::GetInstance()->UpdateObjects(fElapsedTime);

	// Set sound volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nPistolSelectID, COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nShotgunSelectID, COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nRocketSelectID, COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nMusicID, COptionsState::GetInstance()->GetVolume());

	// Health testing
	if(m_pPlayer->gethealth() <= 0.0f)
	{
		m_fPlayerHealth = 0.0f;
		CGame::GetInstance()->PushState(CLooseState::GetInstance());
	}
	if(map->getNodes()->operator[](0)->GetOwner() == OWNER_AI)
	{
		CGame::GetInstance()->PushState(CLooseState::GetInstance());
	}
	if(map->getNodes()->operator[](map->getNodes()->size()-1)->GetOwner() == OWNER_PLAYER && map->getNodes()->operator[](map->getNodes()->size()-1)->isplayerconnected)
		CGame::GetInstance()->PushState(CWinState::GetInstance());
	// Update the node flashing
	m_fFlashTimer += fElapsedTime;
	if(m_fFlashTimer > 1.0f)
	{
		m_bIsFlashing = !m_bIsFlashing;
		m_fFlashTimer = 0.0f;
	}

	// Check collisions between objects in object manager
	ObjectManager::GetInstance()->CheckCollisions();

	// Update the camera
	m_pCamera->Update(m_pPlayer);

	// Update effect
	//effects.update(fElapsedTime);

	// Update ammo notification timers
	if(m_bPistolAmmo)
	{
		m_fPistolTimer += fElapsedTime;
		if(m_fPistolTimer > 1.5f)
			m_bPistolAmmo = false;
	}
	if(m_bShotAmmo)
	{
		m_fShotgunTimer += fElapsedTime;
		if(m_fShotgunTimer > 1.5f)
			m_bShotAmmo = false;
	}
	if(m_bRocketAmmo)
	{
		m_fRocketTimer += fElapsedTime;
		if(m_fRocketTimer > 1.5f)
			m_bRocketAmmo = false;
	}
	

	map->update(fElapsedTime);

	AI.Update(fElapsedTime);

	//effects.update(fElapsedTime);

	statics.update(fElapsedTime);

	playervalid(map);
	
	//Update the Animations
	
	if(Movingdown)
	{
		

		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolDownAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunDownAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketDownAnimationID);
		}
	}
	else if(Movingup)
	{
		
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolUpAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunUpAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketUpAnimationID);
		}
	}
	else if(Movingleft)
	{
		
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolLeftAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunLeftAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketLeftAnimationID);
		}
	}
	else if(Movingright)
	{
		
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolLeftAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunLeftAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketLeftAnimationID);
		}
	}
	else if(ShootingDown)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolShootDownAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunShootDownAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketShootDownAnimID);
		}
	}
	else if(ShootingRight)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolShootLeftAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunShootLeftAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketShootLeftAnimID);
		}
	}
	else if(ShootingUp)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolShootUpAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunShootUpAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketShootUpAnimID);
		}
	}
	else if(ShootingLeft)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, PistolShootLeftAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ShotgunShootLeftAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, RocketShootLeftAnimID);
		}
	}
	else if(GrenadeUp)
	{
		if(GrenadeUp)
		{
			m_fGrenadeTimer += fElapsedTime;
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ThrowGrenadeUpID);

			if(m_fGrenadeTimer > 0.7f)
			{
				GrenadeUp = false;
				m_fGrenadeTimer = 0.0f;
			}
		}
	}
	else if(GrenadeRight)
	{
		if(GrenadeRight)
		{
			m_fGrenadeTimer += fElapsedTime;
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ThrowGrenadeLeftID);
			 
			if(m_fGrenadeTimer > 0.7f)
			{
				GrenadeRight = false;
				m_fGrenadeTimer = 0.0f;
			}
		}
	}
	else if(GrenadeLeft)
	{
		if(GrenadeLeft)
		{
			m_fGrenadeTimer += fElapsedTime;
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ThrowGrenadeLeftID);

			if(m_fGrenadeTimer > 0.7f)
			{
				GrenadeLeft = false;
				m_fGrenadeTimer = 0.0f;
			}
		}
	}
	else if(GrenadeDown)
	{
		if(GrenadeDown)
		{
			m_fGrenadeTimer += fElapsedTime;
			CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, ThrowGrenadeDownID);

			if(m_fGrenadeTimer > 0.7f)
			{
				GrenadeDown = false;
				m_fGrenadeTimer = 0.0f;
			}
		}
	}


	CSGP_EventSystem::GetInstance()->ProcessEvents();
	CSGP_MessageSystem::GetInstance()->ProcessMessages();
}

void CCyberSpaceState::Render(void)
{
	int camOffsetX = CCamera::GetInstance()->GetOffsetX();
	int camOffsetY = CCamera::GetInstance()->GetOffsetY();

	// Render the background
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundID, 0, 0, 1.0f, 1.0f);

	//render the map

	map->render();
	
	// Display number of bullets on screen
	char buffer[128];
	
	/*
	sprintf_s(buffer, _countof(buffer), "Player Health: %i", CPlayer::GetInstance()->gethealth());
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 200, 300, 0, 255, 0);*/

	// Render objects in object manager
	ObjectManager::GetInstance()->RenderObjects();

	////////////////////////////
	//render effects

	//effects.render();

	// Draw the HUD
	CSGD_TextureManager::GetInstance()->Draw(m_nHUDID, int(CGame::GetInstance()->GetScreenWidth()*0.05f), 
		5 - int(CGame::GetInstance()->GetScreenWidth()*0.05f), 0.558f, 0.47f);

	/////////////////////////////////////////////////////////////////////
	// Display the node map
	m_cmMap.Render();
	
	//////////////////////////////////////////////////////////////////////

	// Display player ammo count
	//char buffer[128];

	if(m_nCurrWeapon == PISTOL)
	{
		sprintf_s(buffer, _countof(buffer), "%i", CPlayer::GetInstance()->GetPistolAmmo());
		CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 570, 425, 0, 255, 0);
	}
	else if(m_nCurrWeapon == SHOTGUN)
	{
		sprintf_s(buffer, _countof(buffer), "%i", CPlayer::GetInstance()->GetShotgunAmmo());
		CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 570, 425, 0, 255, 0);
	}
	else if(m_nCurrWeapon == ROCKETS)
	{
		sprintf_s(buffer, _countof(buffer), "%i", CPlayer::GetInstance()->GetRocketAmmo());
		CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 570, 425, 0, 255, 0);
	}

	

	if(GetCurrentGrenade() == EX_GRENADE)
	{
		sprintf_s(buffer, _countof(buffer), "Ex Grenades: %i", CPlayer::GetInstance()->GetExGrenades());
		CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 400, 80, 0, 255, 0);
	}
	else if(GetCurrentGrenade() == FW_GRENADE)
	{
		sprintf_s(buffer, _countof(buffer), "FW Grenades: %i", CPlayer::GetInstance()->GetFwGrenades());
		CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 400, 80, 0, 255, 0);
	}

	RECT rHealth;
	rHealth.left = 0;
	rHealth.top = 0; 
	rHealth.right = long(m_fPlayerHealth*256.0f/100.0f); // Max 256
	rHealth.bottom = 16;

	// Draw Health bar
	CSGD_TextureManager::GetInstance()->Draw(m_nHealthID, 170, 427, 1.27f, 1.0f, &rHealth);
	//CSGD_Direct3D::GetInstance()->DrawRect(rHealth, 0, 255, 0);

	// Current weapon name
	if(m_nCurrWeapon == 0)
		CPrintBitmapFontToScreen::GetInstance()->Print("PISTOL", 530, 405, 0.6f, D3DCOLOR_XRGB(0,255,0));
	else if(m_nCurrWeapon == 1)
		CPrintBitmapFontToScreen::GetInstance()->Print("SHOTGUN", 530, 405, 0.6f, D3DCOLOR_XRGB(0,255,0));
	else if(m_nCurrWeapon == 2)
		CPrintBitmapFontToScreen::GetInstance()->Print("ROCKETS", 530, 405, 0.6f, D3DCOLOR_XRGB(0,255,0));

	// Show ammo pickup notifications
	if(m_bPistolAmmo)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print("PISTOL +10", 500, 320, 0.6f, D3DCOLOR_XRGB(255,0,0));
	}
	if(m_bShotAmmo)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print("SHOTGUN +10", 500, 320, 0.6f, D3DCOLOR_XRGB(255,0,0));
	}
	if(m_bRocketAmmo)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print("ROCKETS +10", 500, 320, 0.6f, D3DCOLOR_XRGB(255,0,0));
	}

	AI.Render();
	// Camera testing
	//////////////
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
	//CSGD_Direct3D::GetInstance()->DrawRect(m_pCamera->GetOuterRect(), 0, 255, 0);
	//CSGD_Direct3D::GetInstance()->DrawRect(m_pCamera->GetInnerRect(), 255, 0, 0);
	//CSGD_Direct3D::GetInstance()->DrawRect(m_pCamera->GetCameraRect(), 0, 255, 0);

	// Render the player
	//CSGD_Direct3D::GetInstance()->DrawRect(CPlayer::GetInstance()->GetCollisionRect(), 0, 0, 255);

	RECT drawRect = m_pPlayer->GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	if(GrenadeDown)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ThrowGrenadeDownID);
	}
	else if(GrenadeLeft)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ThrowGrenadeLeftID);
	}
	else if(GrenadeUp)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ThrowGrenadeUpID);
	}
	else if(GrenadeRight)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-1.0f,1.0f, ThrowGrenadeLeftID);
	}
	else if(ShootingDown)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolShootDownAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunShootDownAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketShootDownAnimID);
		}
	}
	else if(ShootingUp)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolShootUpAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunShootUpAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketShootUpAnimID);
		}
	}
	else if(ShootingLeft)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolShootLeftAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunShootLeftAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketShootLeftAnimID);
		}
	}
	else if(ShootingRight)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-1.0f,1.0f, PistolShootLeftAnimID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-1.0f,1.0f, ShotgunShootLeftAnimID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-1.0f,1.0f, RocketShootLeftAnimID);
		}
	}
	else if(Movingdown)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolDownAnimationID);
		}
		else if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunDownAnimationID);
		}
		else if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketDownAnimationID);
		}
	}
	else if(Movingup)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolUpAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunUpAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketUpAnimationID);
		}
	}
	else if(Movingleft)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolLeftAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunLeftAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketLeftAnimationID);
		}
	}
	else if(Movingright)
	{
		if(GetCurrentWeapon()== PISTOL)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left), drawRect.top,-1.0f,1.0f, PistolLeftAnimationID);
		}
		if(GetCurrentWeapon() == SHOTGUN)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left), drawRect.top,-1.0f,1.0f, ShotgunLeftAnimationID);
		}
		if(GetCurrentWeapon() == ROCKETS)
		{
			CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left), drawRect.top,-1.0f,1.0f, RocketLeftAnimationID);
		}
	}
	else if(!(Movingdown||Movingup||Movingleft||Movingright))
	{
		if(GetCurrentWeapon()== PISTOL)
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, PistolDownAnimationID);
		if(GetCurrentWeapon() == SHOTGUN)
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, ShotgunDownAnimationID);
		if(GetCurrentWeapon() == ROCKETS)
			CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, RocketDownAnimationID);
	}

	if(CSGD_DirectInput::GetInstance()->MouseGetPosX() > CGame::GetInstance()->GetScreenWidth())
		CSGD_DirectInput::GetInstance()->MouseSetPosX(CGame::GetInstance()->GetScreenWidth());

	if(CSGD_DirectInput::GetInstance()->MouseGetPosY() > CGame::GetInstance()->GetScreenHeight())
		CSGD_DirectInput::GetInstance()->MouseSetPosY(CGame::GetInstance()->GetScreenHeight());

	if(CSGD_DirectInput::GetInstance()->MouseGetPosX() < 0)
		CSGD_DirectInput::GetInstance()->MouseSetPosX(0);

	if(CSGD_DirectInput::GetInstance()->MouseGetPosY() < 0)
		CSGD_DirectInput::GetInstance()->MouseSetPosY(0);

	// Draw the aiming reticle
	CSGD_TextureManager::GetInstance()->Draw(m_nReticleID, (CSGD_DirectInput::GetInstance()->MouseGetPosX() - 32), 
		(CSGD_DirectInput::GetInstance()->MouseGetPosY() - 32));


	//CSGD_Direct3D::GetInstance()->DrawLine((int)CPlayer::GetInstance()->GetPosX() - camOffsetX + CPlayer::GetInstance()->GetWidth()*0.5, (int)CPlayer::GetInstance()->GetPosY() - camOffsetY + CPlayer::GetInstance()->GetHeight()*0.5, 
	//	CSGD_DirectInput::GetInstance()->MouseGetPosX(), CSGD_DirectInput::GetInstance()->MouseGetPosY(), 255, 0, 0);

	statics.render();
}

void CCyberSpaceState::Exit(void)
{
	CPlayer::GetInstance()->SetMoneyFactor(0);
	CPlayer::GetInstance()->SetSpeedFactor(0);

	// Unload assets
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nMusicID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nHUDID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nHealthID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nRocketSelectID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nShotgunSelectID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nPistolSelectID);




	ObjectManager::GetInstance()->RemoveAllObjects();
	m_pPlayer->SetPosX((float)CSpaceStationState::GetInstance()->getentx());
	m_pPlayer->SetPosY((float)CSpaceStationState::GetInstance()->getenty());
	m_pPlayer = nullptr;
	
	//map->killmap();

	AI.killai();
}

void CCyberSpaceState::MessageProc(CBaseMessage* pMsg)
{
	switch(pMsg->GetMsgID())
	{
	case MSG_CREATE_PLAYER_BULLET:
		{
			CCreatePlayerBulletMessage* pBulletMess = (CCreatePlayerBulletMessage*)pMsg;
			CPistolBullet* pBullet = (CPistolBullet*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CPistolBullet");
			pBullet->SetPosX(pBulletMess->GetBase()->GetPosX());
			pBullet->SetPosY(pBulletMess->GetBase()->GetPosY());
			// Set bullet's velocity
			if(pBulletMess->GetBase()->GetType() == CBase::OBJ_TURRET)
			{
				
			}
			if(pBulletMess->GetType() == CPistolBullet::BLT_ENEMY)
			{
				tVector2D tTemp;
				tTemp.fX = CCyberSpaceState::GetInstance()->GetPlayer()->GetPosX() - pBulletMess->GetBase()->GetPosX();
				tTemp.fY = CCyberSpaceState::GetInstance()->GetPlayer()->GetPosY() - pBulletMess->GetBase()->GetPosY();

				tTemp = Vector2DNormalize(tTemp);

				pBullet->SetVelX(tTemp.fX*60);
				pBullet->SetVelY(tTemp.fY*60);
				pBullet->SetPosX(pBullet->GetPosX() + (1 * pBullet->GetVelX()));
				pBullet->SetPosY(pBullet->GetPosY() + (1 * pBullet->GetVelY()));
				pBullet->SetPosY(pBullet->GetPosY() + 10);
				pBullet->SetBulletType(CPistolBullet::BLT_ENEMY);

			}
			else
			{
				tVector2D tTemp;
				tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - CPlayer::GetInstance()->GetPosVector().fX;
				tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - CPlayer::GetInstance()->GetPosVector().fY;

				if(CPlayer::GetInstance()->gethealth() < 80)
				{
					// Set random offset
					float angle = CCyberSpaceState::GetInstance()->DegreeToRadian(float(rand()%25));
					tTemp = Vector2DRotate(tTemp, (angle - 0.393f));
				}
				

				tTemp = Vector2DNormalize(tTemp);

				pBullet->SetVelX(tTemp.fX*200);
				pBullet->SetVelY(tTemp.fY*200);

				pBullet->SetBulletType(CPistolBullet::BLT_PLAYER);
			}

			pBullet->SetType(CBase::OBJ_PISTOL_BULLET);
			//pBullet->SetOwner(pBulletMess->GetPlayer());
			ObjectManager::GetInstance()->AddObject(pBullet);
			pBullet->Release();

			CSGP_EventSystem::GetInstance()->SendEvent("bullet.fired");
		}
		break;
	case MSG_DESTROY_PLAYER_BULLET:
		{
			CSGP_EventSystem::GetInstance()->SendEvent("bullet.destroyed");
			CPistolBullet* pBullet = ((CDestroyPlayerBulletMessage*)pMsg)->GetBullet();
			ObjectManager::GetInstance()->RemoveObject(pBullet);
		}
		break;
	case MSG_CREATE_SHOTGUN_BULLET:
		{
			CCreateShotgunBulletMessage* pBulletMess = (CCreateShotgunBulletMessage*)pMsg;
			CShotgunBullet* pBullet = (CShotgunBullet*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CShotgunBullet");
			pBullet->SetPosX(pBulletMess->GetBase()->GetPosX());
			pBullet->SetPosY(pBulletMess->GetBase()->GetPosY());
			// Set bullet's velocity
			tVector2D tTemp;
			tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - CPlayer::GetInstance()->GetPosVector().fX;
			tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - CPlayer::GetInstance()->GetPosVector().fY;

			// Set random offset
			float angle = CCyberSpaceState::GetInstance()->DegreeToRadian(float(rand()%25));
			tTemp = Vector2DRotate(tTemp, (angle - 0.393f));

			tTemp = Vector2DNormalize(tTemp);

			pBullet->SetVelX(tTemp.fX*200);
			pBullet->SetVelY(tTemp.fY*200);

			pBullet->SetBulletType(CPistolBullet::BLT_PLAYER);
			pBullet->SetType(CBase::OBJ_SHOTGUN_BULLET);
			//pBullet->SetOwner(pBulletMess->GetPlayer());
			ObjectManager::GetInstance()->AddObject(pBullet);
			pBullet->Release();
			
		}
		break;
	case MSG_DESTROY_SHOTGUN_BULLET:
		{
			CSGP_EventSystem::GetInstance()->SendEvent("bullet.destroyed");
			CShotgunBullet* pBullet = ((CDestroyShotgunBulletMessage*)pMsg)->GetBullet();
			ObjectManager::GetInstance()->RemoveObject(pBullet);
		}
		break;
	case MSG_CREATE_ROCKET_BULLET:
		{
			CCreateRocketBulletMessage* pBulletMess = (CCreateRocketBulletMessage*)pMsg;
			CRocketLauncherBullet* pBullet = (CRocketLauncherBullet*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CRocketLauncherBullet");
			pBullet->SetPosX(pBulletMess->GetBase()->GetPosX());
			pBullet->SetPosY(pBulletMess->GetBase()->GetPosY());
			// Set bullet's velocity
			tVector2D tTemp;
			tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - CPlayer::GetInstance()->GetPosVector().fX;
			tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - CPlayer::GetInstance()->GetPosVector().fY;

			if(CPlayer::GetInstance()->gethealth() < 80)
			{
				// Set random offset
				float angle = CCyberSpaceState::GetInstance()->DegreeToRadian(float(rand()%25));
				tTemp = Vector2DRotate(tTemp, (angle - 0.393f));
			}

			tTemp = Vector2DNormalize(tTemp);

			pBullet->SetVelX(tTemp.fX*200);
			pBullet->SetVelY(tTemp.fY*200);
			pBullet->SetType(CBase::OBJ_ROCKET_BULLET);
			
			ObjectManager::GetInstance()->AddObject(pBullet);
			pBullet->Release();
		}
		break;
	case MSG_DESTROY_ROCKET_BULLET:
		{
			ObjectManager::GetInstance()->CheckCollisions();
			CSGP_EventSystem::GetInstance()->SendEvent("bullet.destroyed");
			CRocketLauncherBullet* pBullet = ((CDestroyRocketBulletMessage*)pMsg)->GetBullet();
			ObjectManager::GetInstance()->RemoveObject(pBullet);
		}
		break;
	case MSG_CREATE_EXGRENADE:
		{
			CCreateExGrenadeMessage* pGrenMess = (CCreateExGrenadeMessage*)pMsg;
			CGrenade* pGren = (CGrenade*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CGrenade");

			pGren->SetPosX(pGrenMess->GetBase()->GetPosX());
			pGren->SetPosY(pGrenMess->GetBase()->GetPosY());
			tVector2D tTemp;
			tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - CPlayer::GetInstance()->GetPosVector().fX;
			tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - CPlayer::GetInstance()->GetPosVector().fY;

			tTemp = Vector2DNormalize(tTemp);

			pGren->SetVelX(tTemp.fX*200);
			pGren->SetVelY(tTemp.fY*200);
			pGren->SetGrenadeType(CGrenade::EX_GRENADE);
			pGren->SetType(CBase::OBJ_GRENADE);
			
			ObjectManager::GetInstance()->AddObject(pGren);
			pGren->Release();
		}
		break;
	case MSG_CREATE_FWGRENADE:
		{
			CCreateFwGrenadeMessage* pGrenMess = (CCreateFwGrenadeMessage*)pMsg;
			CGrenade* pGren = (CGrenade*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CGrenade");

			pGren->SetPosX(pGrenMess->GetBase()->GetPosX());
			pGren->SetPosY(pGrenMess->GetBase()->GetPosY());
			tVector2D tTemp;
			tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - CPlayer::GetInstance()->GetPosVector().fX;
			tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - CPlayer::GetInstance()->GetPosVector().fY;

			tTemp = Vector2DNormalize(tTemp);

			pGren->SetVelX(tTemp.fX*200);
			pGren->SetVelY(tTemp.fY*200);
			pGren->SetGrenadeType(CGrenade::FW_GRENADE);
			pGren->SetType(CBase::OBJ_GRENADE);

			ObjectManager::GetInstance()->AddObject(pGren);
			pGren->Release();
		}
		break;
	case MSG_DESTROY_GRENADE:
		{
			CGrenade* pGren = ((CDestroyGrenadeMessage*)pMsg)->GetGrenade();
			ObjectManager::GetInstance()->RemoveObject(pGren);
		}
		break;
	case MSG_CREATE_TURRET:
		{
			CCreateTurretMesssage* pTurretMess = (CCreateTurretMesssage*)pMsg;
			CTurret* pTurret = (CTurret*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CTurret");
		
			pTurret->SetPosX(pTurretMess->GetPosX());
			pTurret->SetPosY(pTurretMess->GetPosY());
			pTurret->SetVelX(0.0f);
			pTurret->SetVelY(0.0f);
			pTurret->SetType(CBase::OBJ_TURRET);
			ObjectManager::GetInstance()->AddObject(pTurret);
			pTurret->Release();
		}
		break;
	case MSG_DESTROY_TURRET:
		{
			CDestroyTurretMessage* pTurrDest = (CDestroyTurretMessage*)pMsg;
			ObjectManager::GetInstance()->RemoveObject(pTurrDest->GetTurret());
		}
		break;
	case MSG_CREATE_SPIDER:
		{
			CCreateSpiderMessage* pSpiderMess = (CCreateSpiderMessage*)pMsg;
			CSpider* pSpider = (CSpider*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CSpider");

			pSpider->SetPosX(pSpiderMess->GetPosX());
			pSpider->SetPosY(pSpiderMess->GetPosY());
			pSpider->SetVelX(0.0f);
			pSpider->SetVelY(0.0f);
			pSpider->SetType(CBase::OBJ_SPIDER);
			ObjectManager::GetInstance()->AddObject(pSpider);
			pSpider->Release();
		}
		break;
	case MSG_DESTROY_SPIDER:
		{
			CDestroySpiderMessage* pSpiderDest = (CDestroySpiderMessage*)pMsg;
			ObjectManager::GetInstance()->RemoveObject(pSpiderDest->GetSpider());
		}
		break;
	case MSG_CREATE_PISTOL_DROP:
		{
			CCreatePistolDropMessage* pPistolMess = (CCreatePistolDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pPistolMess->GetBase();
			pDrop->SetPosX(pPistolMess->GetBase()->GetPosX());
			pDrop->SetPosY(pPistolMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::PISTOL);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	case MSG_CREATE_SHOTGUN_DROP:
		{
			CCreateShotgunDropMessage* pShotMess = (CCreateShotgunDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(pShotMess->GetBase()->GetPosX());
			pDrop->SetPosY(pShotMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::SHOTGUN);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	case MSG_CREATE_ROCKET_DROP:
		{
			CCreateRocketDropMessage* pRocketMess = (CCreateRocketDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(pRocketMess->GetBase()->GetPosX());
			pDrop->SetPosY(pRocketMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::ROCKET);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	case MSG_CREATE_HEALTH_DROP:
		{
			CCreateHealthDropMessage* pHealthMess = (CCreateHealthDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(pHealthMess->GetBase()->GetPosX());
			pDrop->SetPosY(pHealthMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::HEALTH);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	/*case MSG_CREATE_EXGRENADE_DROP:
		{
			CCreateExGrenadeDropMessage* pGrenMess = (CCreateExGrenadeDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(pGrenMess->GetBase()->GetPosX());
			pDrop->SetPosY(pGrenMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::EX_GRENADE);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;*/
		case MSG_CREATE_FWGRENADE_DROP:
		{
			CCreateFwGrenadeDropMessage* pGrenMess = (CCreateFwGrenadeDropMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(pGrenMess->GetBase()->GetPosX());
			pDrop->SetPosY(pGrenMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::FW_GRENADE);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	case MSG_DESTROY_DROP:
		{
			CDestroyDropMessage* pDropDest = (CDestroyDropMessage*)pMsg;
			ObjectManager::GetInstance()->RemoveObject(pDropDest->GetDrop());
		}
		break;
	case MSG_DESTROY_FIREWALL:
		{
			CDestroyFirewallMessage* pFireDest = (CDestroyFirewallMessage*)pMsg;
			//ObjectManager::GetInstance()->RemoveObject(pFireDest->GetFirewall());
		}
		break;
	
	}
}

float CCyberSpaceState::DegreeToRadian(float angle)
{
	return D3DX_PI * angle / 180.0f;
}

void CCyberSpaceState::HandleEvent(CEvent* pEvent)
{
	if(pEvent->GetEventID() == "bullet.fired")
	{
		++m_nBulletsOnScreen;
	}
	if(pEvent->GetEventID() == "bullet.destroyed")
	{
		--m_nBulletsOnScreen;
	}
}

void CCyberSpaceState::playervalid(CMap* themap)
{
	unsigned int size = themap->getcurNode()->getTeles()->size();
	for(unsigned int i = 0; i < size;i++)
	{
		if(themap->getcurNode()->getTeles()->operator[](i)->getLink()->getNode()->isplayerconnected || themap->getcurNode() == themap->getNodes()->operator[](0))
			return;
	}
		std::vector<CNode*> valid;
	
		for(unsigned int i = 0; i < themap->getNodes()->size(); i++)
		{
			if(themap->getNodes()->operator[](i)->GetOwner() == OWNER_PLAYER && themap->getNodes()->operator[](i)->isplayerconnected)
			{
				valid.push_back(themap->getNodes()->operator[](i));
			}
		}
		if(valid.size() > 0)
		{
		
			map->getNodes()->operator[](0)->getTeles()->operator[](0)->getLink()->Teleport();
			return;

		}

		
		/*if(valid.size() > 1)
		{
			int selected;
			if(valid.size() > 1)
			{
				for(unsigned int i = 0; i < map->getNodes()->size(); i++)
				{
					if(map->getNodes()->operator[](i)->isplayerconnected == true)
					{
						selected = i;
					}

				}
			}
			map->getcurNode()->ExitNode();
			CPlayer::GetInstance()->SetPosX(map->getNodes()->operator[](selected)->getTeles()->operator[](0)->GetPosX());
			CPlayer::GetInstance()->SetPosY(map->getNodes()->operator[](selected)->getTeles()->operator[](0)->GetPosX());



			map->getNodes()->operator[](selected)->EnterNode();
		}*/


	
}

CNode* CCyberSpaceState::getnode()
{
	return map->getcurNode();
}